package it.unisannio.lesim.fsm;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;

/**
 *
 * @author Marco Calisti
 * @author Antonio Cuomo
 * @author Rocco Mazzeo
 * @author Francesco Pacilio
 * 
 */

public class FSM {

    private State currentState;
    private Hashtable states;
    private Message lastMessage;
    private String name,  description;
    private Hashtable storage;
    
    public FSM(String name, String description) {
        this.name = name;
        this.description = description;
        this.states = new Hashtable();
        this.lastMessage = null;
        this.storage= new Hashtable();
    }

    public String getDescription() {
        return description;
    }

    public String getName() {
        return name;
    }
    
    public State getCurrentState() {
        return currentState;
    }
    
    public Map getStorage(){
        return storage;
    } 
    public Enumeration getStates() {
        return states.elements();
    }
    
    public Hashtable getHashStates() {
        return states;
    }
    
    public void setFirstState(String state) {
        currentState = (State) states.get(state);
    }
    
    public void setStates(Hashtable states) {
        this.states=states;
    }

    public void addState(State s) {
        states.put(s.getName(), s);
    }

    public void receiveMessage(Message m) {
        if (lastMessage == null) {
            lastMessage = m;
        }

        State lastCurrentState = currentState;
        currentState = (State) states.get(currentState.receiveMessage(m, lastMessage));
        if (lastCurrentState != currentState) {
            lastMessage = m;
        }
    }
    
}
